package com.tony.business.config;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;

/**
 * @author tony
 * @date 2020/2/20 10:48 上午
 */
@Configuration
@MapperScan(basePackages = DataSourceConfig.BASE_PACKAGE,sqlSessionTemplateRef = "SqlSessionTemplate")
public class DataSourceConfig {

    static final String BASE_PACKAGE = "com.tony.mapper.mapper";
    static final String MAPPER_LOCAL = "classpath*:mapper/*.xml";
    static final String TYPE_ALIASES_PACKAGE = "com.tony.mapper.domain.po";
    static final String TYPE_ENUM_PACKAGE = "com.tony.domain.enumerate";


    @Bean("datasource")
    @Primary
    @ConfigurationProperties(prefix = "spring.hikari")
    public DataSource dataSource() {
        return new HikariDataSource();
    }

    @Bean("sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory1(@Qualifier("datasource")DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(MAPPER_LOCAL));
        sqlSessionFactory.setTypeAliasesPackage(TYPE_ALIASES_PACKAGE);
        sqlSessionFactory.setTypeEnumsPackage(TYPE_ENUM_PACKAGE);
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        configuration.setMapUnderscoreToCamelCase(true);
        sqlSessionFactory.setConfiguration(configuration);
        sqlSessionFactory.setGlobalConfig(globalConfiguration());
        sqlSessionFactory.setPlugins(new PaginationInterceptor());
        return sqlSessionFactory.getObject();

    }

    @Bean("SqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    public GlobalConfig globalConfiguration() {
        GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
        dbConfig.setIdType(IdType.AUTO);
        dbConfig.setLogicDeleteField("is_del");
        dbConfig.setLogicDeleteValue("2");
        dbConfig.setLogicNotDeleteValue("1");

        GlobalConfig conf = new GlobalConfig();
        conf.setBanner(false);
        conf.setDbConfig(dbConfig);
        return conf;
    }

    @Bean(name = "transactionManager")
    @Primary
    public DataSourceTransactionManager master2TransactionManager(@Qualifier("datasource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

}
